<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\model;
use think\Model;
use think\facade\Db;
use app\common\Wechat;
class Member extends Model
{
	//添加会员
	function add($aid,$data){
		if(!$data['levelid']){
			$data['levelid'] = Db::name('member_level')->where('aid',$aid)->where('isdefault',1)->value('id');
		}
		if($data['pid']){
			$parent = Db::name('member')->where('aid',$aid)->where('id',$data['pid'])->find();
			if(!$parent){
				$data['pid'] = 0;
			}else{
				if($parent['path']){
					$data['path'] = $parent['path'] . ',' .$parent['id'];
				}else{
					$data['path'] = ''.$parent['id'];
				}
				}
		}else{
			$data['path'] = '';
		}
		$data['random_str'] = random(16);
		$set = Db::name('admin_set')->where('aid',$aid)->find();
		if($set['reg_invite_code'] != 0 && $set['reg_invite_code_type']==1){
			$data['yqcode'] = self::getyqcode($aid);
		}
		if($set['reg_check'] == 1) $data['checkst'] = 0;
		$member = self::create($data);
		return $member->id;
	}
	function getyqcode($aid){
		$code = rand(100000,999999);
		$member = Db::name('member')->where('aid',$aid)->where('yqcode',$code)->find();
		if($member){
			return self::getyqcode($aid);
		}
		return $code;
	}
	//修改会员
	function edit($aid,$data){
		$member = self::where('aid',$aid)->where('id',$data['id'])->find();
		if(!$member) return ['status'=>0,'msg'=>t('会员').'不存在'];
		$oldpath = $member['path'];
		if(isset($data['pid'])){
			$parent = Db::name('member')->where('aid',$aid)->where('id',$data['pid'])->find();
			if($parent['pid'] == $member['id']) return ['status'=>0,'msg'=>'不能互为上下级关系'];
			if(!$parent){
				$data['pid'] = 0;
			}else{
				if($parent['path']){
					$data['path'] = $parent['path'] . ',' .$parent['id'];
				}else{
					$data['path'] = ''.$parent['id'];
				}
			}
		}else{
			$data['path'] = '';
		}
		if(!$data['aid']) $data['aid'] = $aid;
		if(!$data['random_str']) $data['random_str'] = random(16);
		$member->save($data);
		if($data['path'] != $oldpath){
			$allpath = trim($oldpath.','.$data['path'].','.$member->id);
			self::updatepath($allpath);
		}
		\app\common\Wechat::updatemembercard($aid,$data['id']);
		 return ['status'=>1,'msg'=>'修改成功'];
	}
	//删除会员
	function del($aid,$mids){
		if(!$mids) return ['status'=>0,'msg'=>'没有要删除的会员'];
		$memberlist = self::where('aid',aid)->where('id','in',$mids)->select();
		foreach($memberlist as $member){
			$downlist = self::where('aid',aid)->where('find_in_set('.$member['id'].',path)')->select();
			foreach($downlist as $down){
				if($down['pid'] == $member['id']){
					$down->pid = $member['pid'];
					$down->path = $member['path'];
				}else{
					$pathArr = explode(',',$down['path']);
					foreach($pathArr as $k=>$v){
						if($v == $member['id']){
							unset($pathArr[$k]);
						}
					}
					$down->path = implode(',',$pathArr);
				}
				$down->save();
			}
			self::where('aid',aid)->where('id',$member['id'])->delete();
            Db::name('member_level_record')->where('aid',aid)->where('mid',$member['id'])->delete();
            Db::name('member_favorite')->where('aid',aid)->where('mid',$member['id'])->delete();
            Db::name('member_address')->where('aid',aid)->where('mid',$member['id'])->delete();
            Db::name('member_history')->where('aid',aid)->where('mid',$member['id'])->delete();
            Db::name('member_poster')->where('aid',aid)->where('mid',$member['id'])->delete();
            Db::name('kefu_message')->where('aid',aid)->where('mid',$member['id'])->delete();
		}
		return ['status'=>1,'msg'=>'删除成功'];
	}

	
	//会员path
	function updatepath($pathstr){
		if(!$pathstr) return;
		$pathArr = explode(',',$pathstr);
		foreach($pathArr as $pid){
			if(!$pid) continue;
			$memberlist = Db::name('member')->where("find_in_set({$pid},path)")->order('id')->select()->toArray();
			foreach($memberlist as $member){
				$pathArr = self::getpath($member,$path=[]);
				if($pathArr){
					$pathArr = array_reverse($pathArr);
					$pathstr = implode(',',$pathArr);
					Db::name('member')->where('id',$member['id'])->update(['path'=>$pathstr]);
				}else{
					Db::name('member')->where('id',$member['id'])->update(['path'=>'']);
				}
			}
		}
	}
	function getpath($member,$path,$deep=1){
		if($member['pid'] && $deep < 20){
			if(in_array($member['pid'],$path)) return $path;
			$path[] = $member['pid'];
			$parent = Db::name('member')->where('id',$member['pid'])->find();
			$deep = $deep +1;
			return self::getpath($parent,$path,$deep);
		}
		return $path;
	}
	public function send_free_notice($data){

    	}
}